package com.wj.rpsm.user.controller;

import com.wj.rpsm.user.pojo.Pay;
import com.wj.rpsm.user.pojo.vo.ResponsePojo;
import com.wj.rpsm.user.pojo.constant.FreightBasisData;
import com.wj.rpsm.user.pojo.vo.UnpaidOwner;
import com.wj.rpsm.user.service.AmountService;
import com.wj.rpsm.user.service.PayService;
import com.wj.rpsm.user.utils.DateUtil;
import io.swagger.annotations.*;
import jdk.nashorn.internal.ir.annotations.Ignore;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


/**
 * @author WeiJin
 * @version 1.0
 */
@RestController
@RequestMapping("/fee")
@Api(tags = "费用相关api")
public class FeeController {

    @Autowired
    private AmountService amountService;
    @Autowired
    private PayService payService;
    @Autowired
    private FreightBasisData freightBasisData;

    /**
     * 根据用户id查询欠费信息
     *
     * @param id
     * @return
     */
    @ApiOperation("查询欠费信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "current", type = "Integer"),
            @ApiImplicitParam(name = "size", type = "Integer"),
            @ApiImplicitParam(name = "id", type = "Integer", required = true, dataType = "path"),
    })
    @GetMapping("/unPaid/{id}")
    public ResponsePojo<List<UnpaidOwner>> unPaidFee(@PathVariable("id") Integer id) {
        List<UnpaidOwner> result = amountService.unPaidFee(id);
        return ResponsePojo.success(result);

    }

    /**
     * 缴费
     *
     * @param p
     * @return
     */
    @ApiOperation(value = "缴费", notes = "新增缴费记录和修改余额")
    @PostMapping("/addPay")
    public ResponsePojo<Integer> addPay(@RequestBody @Ignore Pay p) {
        //金额乘以100
        p.setPay(p.getPay() * 100);
        p.setPay_date(DateUtil.getNowDate());
        Integer result = payService.insertPay(p);
        if (result == 2) {
            return ResponsePojo.success(result);
        } else {
            return ResponsePojo.fail(result, "缴费失败");
        }

    }


    /**
     * 查询费用说明
     *
     * @return
     */
    @ApiOperation(value = "查询计费标准")
    @ApiParam
    @GetMapping("/queryAll/basis")
    public ResponsePojo<FreightBasisData> queryAll() {
        return ResponsePojo.success(freightBasisData);
    }



    @ApiOperation("缴费明细")
    @ApiImplicitParam(name = "id", type = "Integer", required = true, paramType = "path")
    @GetMapping("/details/{id}")
    public ResponsePojo<List<Pay>> paymentDetails(@PathVariable("id") Integer id) {

        return ResponsePojo.success(payService.pageOfDetailsByUserId(id));
    }
}
